﻿
#region Usings

using PolyGameEngine.GamePieces.Common;
using PolyGameEngine.Tools;

using FarseerPhysics.Dynamics;
using FarseerPhysics.Dynamics.Contacts;

using Microsoft.Xna.Framework;

#endregion

namespace PolyGameEngine.GamePieces
{
    public abstract class Unit : GamePiece
    {
        public bool canJump = false;
        public bool isNormalJumpUp = true;

        public Vector2 MaxLinearVelocity = new Vector2(800, 800);
        public float MaxAngularVelocity = 1000;

        public Unit(PhysicsGame PhysicsGame, World World, Material Material, Vector2 Position, float Rotation, float DrawingDepth, int saveLineIndex, string GPID)
            : base(PhysicsGame, World, Material, Position,Rotation, DrawingDepth, saveLineIndex, GPID)
        {
            bodyType = BodyType.Dynamic;
        }

        public override void Initialize()
        {
            body.OnCollision += HandleDefaultCollision;
            base.Initialize();
        }

        public override void Update(GameTime gameTime)
        {
            HandleInput();
            canJump = false;
            if (Velocity.X >= MaxLinearVelocity.X)
                Velocity = new Vector2(MaxLinearVelocity.X, Velocity.Y);
            if (Velocity.Y >= MaxLinearVelocity.Y)
                Velocity = new Vector2(Velocity.X, MaxLinearVelocity.Y);
            if (AngularVelocity >= MaxAngularVelocity)
                AngularVelocity = MaxAngularVelocity;
            if (AngularVelocity <= -MaxAngularVelocity)
                AngularVelocity = -MaxAngularVelocity;
               
            base.Update(gameTime);
        }

        public override bool HandleDefaultCollision(Fixture ThisFixture, Fixture OtherFixture, Contact Contact)
        {
            if (OtherFixture.Body.Position.Y >= ThisFixture.Body.Position.Y)
            {
                isNormalJumpUp = true;
            }
            else
                isNormalJumpUp = false;

            return base.HandleDefaultCollision(ThisFixture, OtherFixture, Contact);
        }

        public override void Draw(GameTime gameTime)
        {
            base.Draw(gameTime);
        }

        public abstract void HandleInput();

    }
}
